package priv.mill.controller;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import priv.mill.controller.util.RetenObj;
import priv.mill.entry.Complain;
import priv.mill.server.ComplainService;

@Controller
@RequestMapping("Complain")
public class ComplainController {

	@Autowired
	private ComplainService complainService;

	private static final Logger logger = Logger.getLogger(ComplainController.class);

	@RequestMapping("getComplainById")
	@ResponseBody
	public RetenObj getComplainById(@RequestParam("ComplainId") Integer complainId) {
		try {
			return warpData(complainService.getComplainById(complainId), 200);
		} catch (Exception e) {
			logger.error(e.getMessage());
			return warpData(null, 500);
		}
	}

	private RetenObj warpData(Object object, int i) {
		RetenObj retenObj = new RetenObj();
		retenObj.setData(object);
		retenObj.setStatus(i);
		return retenObj;
	}

	@RequestMapping("getComplainByEntry")
	@ResponseBody
	public RetenObj getComplainByEntry(Complain complain) {
		try {
			return warpData(complainService.getComplainByEntry(complain), 200);
		} catch (Exception e) {
			logger.error(e.getMessage());
			return warpData(null, 500);
		}
	}

	@RequestMapping("getComplainsByPage")
	@ResponseBody
	public RetenObj getComplainsByPage(Complain complain) {
		try {
			return warpData(complainService.getComplainsByPage(complain), 200);
		} catch (Exception e) {
			logger.error(e.getMessage());
			return warpData(null, 500);
		}
	}

	@RequestMapping("updateComplain")
	@ResponseBody
	public RetenObj updateComplain(Complain complain) {
		try {
			int result = complainService.updateComplain(complain);
			if (result > 0)
				return warpData(result, 200);
			else
				return warpData(result, 300);
		} catch (Exception e) {
			logger.error(e.getMessage());
			return warpData(null, 500);
		}
	}
	

	@RequestMapping("insertComplain")
	@ResponseBody
	public RetenObj insertComplain(Complain complain) {
		try {
			int result = complainService.insertComplain(complain);
			if (result > 0)
				return warpData(complain.getComplainId(), 200);
			else
				return warpData(result, 300);
		} catch (Exception e) {
			logger.error(e.getMessage());
			return warpData(null, 500);
		}
	}

	@RequestMapping("deleteComplain")
	
	@ResponseBody
	public RetenObj deleteComplain(Integer complainId) {
		try {
			int result = complainService.deleteComplain(complainId);
			if (result > 0)
				return warpData(result, 200);
			else
				return warpData(result, 300);
		} catch (Exception e) {
			logger.error(e.getMessage());
			return warpData(null, 500);
		}

	}
}
